<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="评价日期" prop="evaluationDate">
        <el-date-picker clearable
                        v-model="queryParams.evaluationDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择评价日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="评价处理人" prop="badHandlePerson">
        <el-input
          v-model="queryParams.badHandlePerson"
          placeholder="请输入评价处理人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['extend:evaluation:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="evaluationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="客户名称" align="center" prop="customerName"/>
      <el-table-column label="分段订单" align="center" prop="ledId"/>
      <el-table-column label="评价日期" align="center" prop="evaluationDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.evaluationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="serviceRating" >
        <template #default="scope">
          <span class="demonstration">服务评分:</span><el-rate v-model="scope.row.serviceRating" :colors="colors"></el-rate>
          <span class="demonstration">时效评分:</span><el-rate v-model="scope.row.timelinessRating" :colors="colors"></el-rate>
          <span class="demonstration">货品评分:</span><el-rate v-model="scope.row.productRating" :colors="colors"></el-rate>
        </template>
      </el-table-column>
      <el-table-column label="评价内容" align="center" prop="comment"/>
      <el-table-column label="评价处理状态" align="center" prop="badStatus">
        <template #default="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.badStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="评价处理人" align="center" prop="badHandlePerson"/>
      <el-table-column label="评价处理时间" align="center" prop="badHandleTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.badHandleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['extend:evaluation:edit']"
          >处置
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单评价对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="10" class="mb8">
          <el-col :span="12">
            <el-form-item label="客户名称" prop="evaluationDate">
              <el-input v-model="form.customerName"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评价日期" prop="evaluationDate">

                <el-date-picker clearable
                                v-model="form.evaluationDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择评价日期"
                >
                </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
          <el-form-item label="评价内容" prop="comment">
            <el-input v-model="form.comment" type="textarea" placeholder="请输入内容"/>
          </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="评分">
              <span class="demonstration">服务评分:</span><el-rate v-model="form.serviceRating" :colors="colors"></el-rate>
              <span class="demonstration">时效评分:</span><el-rate v-model="form.timelinessRating" :colors="colors"></el-rate>
              <span class="demonstration">货品评分:</span><el-rate v-model="form.productRating" :colors="colors"></el-rate>
            </el-form-item>
          </el-col>
          <el-col :span="12">
          <el-form-item label="评价回复" prop="badReturn">
            <el-input v-model="form.badReturn" type="textarea" placeholder="请输入内容"/>
          </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.badStatus === 'N'">
          <el-form-item label="评价处理人" prop="badHandlePerson">
            <el-input v-model="form.badHandlePerson" placeholder="请输入评价处理人" readonly="readonly"/>
          </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.badStatus === 'N'">
          <el-form-item label="评价处理时间" prop="badHandleTime">
            <el-date-picker clearable
                            v-model="form.badHandleTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择评价处理时间"
                            readonly="readonly"
            >
            </el-date-picker>
          </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="form.badStatus === 'N'">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addEvaluation, delEvaluation, getEvaluation, listEvaluation, updateEvaluation } from '@/api/extend/evaluation'

export default {
  name: 'LedEvaluation',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单评价表格数据
      evaluationList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        evaluationDate: null,
        serviceRating: null,
        timelinessRating: null,
        productRating: null,
        comment: null,
        badStatus: null,
        badReturn: null,
        badHandlePerson: null,
        badHandleTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: '订单唯一标识外键不能为空', trigger: 'blur' }
        ],
        serviceRating: [
          { required: true, message: '服务评分，取值范围 1-5不能为空', trigger: 'blur' }
        ],
        timelinessRating: [
          { required: true, message: '时效性评分，取值范围 1-5不能为空', trigger: 'blur' }
        ],
        productRating: [
          { required: true, message: '货品评分，取值范围 1-5不能为空', trigger: 'blur' }
        ]
      },
      colors: ['#99A9BF', '#F7BA2A', '#FF9900']
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询订单评价列表 */
    getList() {
      this.loading = true
      listEvaluation(this.queryParams).then(response => {
        this.evaluationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        evaluationDate: null,
        serviceRating: null,
        timelinessRating: null,
        productRating: null,
        comment: null,
        badStatus: null,
        badReturn: null,
        badHandlePerson: null,
        badHandleTime: null,
        createBy: null,
        createTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加订单评价'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getEvaluation(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改订单评价'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEvaluation(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addEvaluation(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除订单评价编号为"' + ids + '"的数据项？').then(function() {
        return delEvaluation(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('extend/evaluation/export', {
        ...this.queryParams
      }, `evaluation_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
